﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;


namespace WindowsFormsApplication3
{
    public partial class PatientForm : Form
    {
        public Patient p = new Patient();
        
        public PatientForm()
        {
            InitializeComponent();
        }

        public void button1_Click(object sender, EventArgs e)
        { 
                p.Name = textBox1.Text;
                p.SurName = textBox2.Text;
                p.Patronymic = textBox4.Text;
                //p.dateOfBirth = Convert.ToDateTime(dateTimePicker1);
                //ToString("dd/MM/yyyy ")

        }

        private void PatientForm_Load(object sender, EventArgs e)
        {

        }

        private void PatientForm_KeyPress(object sender, KeyPressEventArgs e)
        {
           
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_KeyUp(object sender, KeyEventArgs e)
        {

        }
}

    public class DataAccess
    {

        public void AppPatient(Patient p)
        {


            string idd = "";
            string[] str1 = File.ReadAllLines(".\\..\\..\\Patient_text.txt");

            if (str1.Length == 0)
            {
                p.id = 1;
            }
            else
            {
                idd = str1[str1.Length - 1].Substring(0, str1[str1.Length - 1].IndexOf(","));
                p.id = Convert.ToInt32(idd);
                p.id++;
            }
            //System.Console.WriteLine(str1.Length);

            //System.Console.WriteLine(str1.Length);
            string str = p.id.ToString() + ",";
            str += p.Name + ",";
            str += p.SurName + ",";
            str += p.Patronymic + ",";
          //  str += p.dateOfBirth.ToString("dd/MM/yyyy ");


            File.AppendAllText(".\\..\\..\\Patient_text.txt", str + "\n");


        }

        public List<Patient> list()
        {
            List<Patient> list = new List<Patient>();
            string[] str1 = File.ReadAllLines(".\\..\\..\\Patient_text.txt");
            string[] k;
            for (int i = 0; i < str1.Length; i++)
            {
                Patient patient1 = new Patient();
                k = str1[i].Split(',');
                patient1.id = Convert.ToInt32(k[0]);
                patient1.Name = k[1];
                patient1.SurName = k[2];
                patient1.Patronymic = k[3];
                //patient1.dateOfBirth = Convert.ToDateTime(k[4]);
                list.Add(patient1);
            }
            return list;
        }


        public void UpdatePatient(Patient p)
        {
            List<Patient> list = new List<Patient>();
            string[] str1 = File.ReadAllLines(".\\..\\..\\Patient_text.txt");
            string[] k = { };
            File.WriteAllText(".\\..\\..\\Patient_text.txt", "");
            PatientForm pf = new PatientForm();
            for (int i = 0; i < str1.Length; i++)
            {
                Patient patient1 = new Patient();
                k = str1[i].Split(',');
                if (Convert.ToInt32(k[0]) == p.id)
                {
                    p.Name = pf.textBox1.Text;
                    p.SurName = pf.textBox2.Text;
                    p.Patronymic = pf.textBox4.Text;
                    str1[i] = p.id + "," + p.Name + "," + p.SurName + "," + p.Patronymic + "," + p.dateOfBirth.ToString("dd/MM/yyyy ");
                    //System.Console.WriteLine(str1[i]);
                }

                File.AppendAllText(".\\..\\..\\Patient_text.txt", str1[i] + "\n");
            }
        }


          public void deletePatient(Patient p)
          {
              List<Patient> list = new List<Patient>();

              string[] str1 = File.ReadAllLines(".\\..\\..\\Patient_text.txt");

              string[] k;
             // MessageBox.Show(p);

              for (int i = 0; i < str1.Length; i++)
              {
                  k = str1[i].Split(',');
                  if (Convert.ToInt32(k[0]) != p.id)
                  {
                      Patient patient1 = new Patient();
                      k = str1[i].Split(',');
                      patient1.id = Convert.ToInt32(k[0]);
                      patient1.Name = k[1];
                      patient1.SurName = k[2];
                      patient1.Patronymic = k[3];
                      //patient1.dateOfBirth = Convert.ToDateTime(k[4]);
                      list.Add(patient1);
                  }
              }

              File.WriteAllText(".\\..\\..\\Patient_text.txt", "");
              for (int i = 0; i < list.Count; i++)
              {
                  File.AppendAllText(".\\..\\..\\Patient_text.txt", list[i].id + "," + list[i].Name + "," + list[i].SurName + "," + list[i].Patronymic + ", " + "\n");
              }

          }

    }
}
